/*
 * $Id: DirNode.java,v 1.5 2009/07/13 16:16:05 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.pojo;

import java.io.File;
import java.util.Hashtable;

import com.ge.healthcare.autosc.common.ASCLogger;

public class DirNode {

	public static final String PATH_SEP = "|";
	private File currentDir = null;
	// Name of the level
	private String name = null;
	private Hashtable<String, DirNode> children = new Hashtable<String, DirNode>();

	public DirNode(String parentDir, String dirname) {
		File dirFile = new File(parentDir, dirname);
		synchronized (DirNode.class) {
			if ((dirFile.mkdirs() == true) || (dirFile.exists())) {
				name = dirname;
				currentDir = dirFile;
			} else {
				ASCLogger.info(this.getClass(), "DirNode",
						"Failed to create directory "
								+ dirFile.getAbsolutePath());
			}
		}
	}

	public DirNode(File parentDir, String dirname) {
		File dirFile = new File(parentDir, dirname);
		// make it thread and process safe
		synchronized (DirNode.class) {
			if ((dirFile.mkdirs() == true) || (dirFile.exists())) {
				name = dirname;
				currentDir = dirFile;
			} else {
				ASCLogger.info(this.getClass(), "DirNode",
						"Failed to create directory "
								+ dirFile.getAbsolutePath());
			}
		}
	}

	public DirNode(File dir) {
		synchronized (DirNode.class) {
			if ((dir.mkdirs() == true) || (dir.exists())) {
				name = dir.getName();
				currentDir = dir;
			} else {
				ASCLogger.info(this.getClass(), "DirNode",
						"Failed to create directory " + dir.getAbsolutePath());
			}
		}
	}

	public DirNode addChild(String dirname) {
		DirNode childNode = null;
		if (currentDir != null) {
			childNode = new DirNode(currentDir, dirname);
			children.put(dirname, childNode);
		}
		return childNode;
	}

	public File getFile() {
		return currentDir;
	}

	/**
	 * Expect the path is in the format of xxx|yyy|zzz
	 * 
	 * @param path
	 */
	public DirNode findChild(String path) {
		DirNode retNode = null;
		int index = path.indexOf(PATH_SEP);
		String child = null;
		String grandChildren = null;
		if (index != -1) {
			child = path.substring(0, index);
			grandChildren = path.substring(index + 1);
		} else {
			// this is the last string
			child = path;
		}
		if (children.containsKey(child) == true) {
			DirNode childNode = children.get(child);
			if (grandChildren != null) {
				retNode = childNode.findChild(grandChildren);
			} else {
				// this is the one
				retNode = childNode;
			}
		} else {
			// Invalid path
			ASCLogger.warn(this.getClass(), "findChild",
					"Cannot find the child, path is invalid");
		}
		return retNode;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
}
